import datetime

from info import db
import random

from info.models import User
from manager import app


def add_user():
    now = datetime.datetime.now()
    users = []
    for num in range(1, 10000):
        try:
            user = User()
            user.nick_name = '%011d' % num
            user.mobile = random.randint(13000000000, 18000000000)
            user.password_hash = "pbkdf2:sha256:50000$xi6Ys6GW$f51f676a3387a6f2c9fc6ae0a3b356a5feb6c5e4fc37c38fdcfca2c9bd19c346"
            user.last_login = now - datetime.timedelta(seconds=random.randint(0, 2678400))
            users.append(user)
            print(user.mobile)
        except Exception as e:
            print(e)
    with app.app_context():
        try:
            db.session.add_all(users)
            db.session.commit()
        except Exception as e:
            print('创建失败')
            db.session.rollback()
    print('创建完成')


if __name__ == '__main__':
    add_user()
